package com.wskj.library.dao.impl;

import com.wskj.library.pojo.*;

import com.wskj.library.dao.*;

import java.util.List;

/**
 * userInfo dao接口类实现类
 * 2018-03-22 T148
 */ 
public class UserInfoDaoImpl extends BaseDao implements UserInfoDao{

	 @Override
	 public int addUserInfo(UserInfo  u){
		 String sql="insert into userinfo (uname, sex,birthday,address,deposite,registerTime,effectTime,account) values (?, ?, ?, ?, ?,?,?,?);";
		 Object[] params= {u.getUname(),u.getSex(),u.getBirthday(),u.getAddress(),u.getDeposite(),u.getRegisterTime(),u.getEffectTime(),u.getAccount()};
		 return super.executeUpdate(sql, params);
	 }
	 
	 @Override
	 public int deleteById(int id){
		 String sql="delete from userInfo  where uid=? ";
		 Object[] params= {id};
		 return super.executeUpdate(sql, params);
	 }
	 
	 @Override
	 public int updateUserInfo(UserInfo u){
		 String sql="update userinfo set uname=?,sex=?,birthday=?,address=?,deposite=?,effectTime=?" 
	               + "  where uid=?";
		 Object[] params= {u.getUname(),u.getSex(),u.getBirthday(),u.getAddress(),u.getDeposite(),u.getEffectTime(),u.getUid()};
		 return super.executeUpdate(sql, params);
	 }
	 
	 @Override
	 public UserInfo getUserInfoById(int id){
		 String sql="select * from userInfo where uid=? ";
		 Object[] params= {id};
		 return super.queryOne(sql, params,UserInfo.class);
	 }
	 
	 @Override
	 public  List<UserInfo>  getAllUserInfos(){
		 String sql="select * from userInfo";
		 Object[] params= null;
		 return super.queryAll(sql, params, UserInfo.class);
	 }	
	 
	@Override
	public UserInfo checkLogin(String name, String pass) {
		 String sql="select * from userInfo where account=? and pass=? ";
		 Object[] params= {name,pass};
		return super.queryOne(sql, params,UserInfo.class);
	}
	
	@Override
	public long getCountsByName(String name, String deposite, String address,String sex) {
		String sql="select count(*) from userinfo where uname like concat('%',?,'%') and "
				+ " address like concat('%',?,'%') and (deposite>? or ''=? ) "
				+ " and (sex=? or ''=? )";
		Object[] params= {name,address,deposite,deposite,sex,sex};
		return super.getCounts(sql, params);
	}
	
	
	
	@Override
	public List<UserInfo> getUserWithPage(String name, String deposite, String address, String sex, int index,
			int pageSize) {		
		String sql="select * from userinfo where uname like concat('%',?,'%') and "
				+ " address like concat('%',?,'%') and (deposite>? or ''=? ) "
				+ " and (sex=? or ''=? ) limit ?,?";
		Object[] params= {name,address,deposite,deposite,sex,sex,(index-1)*pageSize,pageSize};
		
		return super.queryAll(sql, params, UserInfo.class);
	}
	
}

